
package com.borqs.music.model;

import static com.borqs.music.MusicApplication.MD5_NULL;

import android.os.Parcel;
import android.os.Parcelable;

public class SongInfo implements Parcelable {

    public int id = 0;
    public String fileName = "";
    public String fileType = "";
    public String fileUrl = "";
    public String fileMd5 = MD5_NULL;
    public long fileSize = 0;
    public long dateAdded = 0;

    // ID3 Tags
    public String title = "";
    public String artist = "";
    public String album = "";
    public String year = "";
    public String genre = "";
    public String lyrics = "";
    public String imgUrl = "";
    public boolean isFavorite = false;
    public boolean isNotInDevice = false;
    public boolean isPublicSong = false;
    public boolean isLocalLyric = false;  //Lyric and song in the same folder.

    public int bitrate = 0;
    public int duration = 0;

    public String printMP3Info() {
        return "artist:" + artist + " title:" + title + " album:" + album
                + " year:" + year + " lyrics:" + lyrics;
    }

    /**
     * Judge same songs: Must title/artist is same or filename/filesize is same
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof SongInfo)) {
            return false;
        }
        SongInfo that = (SongInfo) o;
        /*
        if (title != null ? !title.equals(that.title) : that.title != null) {
            return false;
        }
        if (artist != null ? !artist.equals(that.artist) : that.artist != null) {
            return false;
        }
        if (fileName != null ? !fileName.equals(that.fileName) : that.fileName != null) {
            return false;
        }
        if (fileSize != that.fileSize) {
            return false;
        }
        */
        /*if ((title.equals(that.title) && artist.equals(that.artist)) ||
                (fileName.equals(that.fileName) && fileSize == that.fileSize)) {
            return true;
        }*/
        if (fileMd5.equals(that.fileMd5)) {
            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {
        int result = 0;
        result = 31 * result + duration;
        result = 31 * result + (title == null ? 0 : title.hashCode());
        result = 31 * result + (artist == null ? 0 : artist.hashCode());
        return result;
    }

    public static final Parcelable.Creator<SongInfo> CREATOR = new Parcelable.Creator<SongInfo>() {
        @Override
        public SongInfo createFromParcel(Parcel in) {
            return new SongInfo(in);
        }

        @Override
        public SongInfo[] newArray(int size) {
            return new SongInfo[size];
        }
    };

    public SongInfo() {

    }

    private SongInfo(Parcel in) {
        id = in.readInt();
        fileName = in.readString();
        fileType = in.readString();
        fileUrl = in.readString();
        fileMd5 = in.readString();
        fileSize = in.readLong();
        dateAdded = in.readLong();

        // ID3 Tags
        title = in.readString();
        artist = in.readString();
        album = in.readString();
        year = in.readString();
        genre = in.readString();
        lyrics = in.readString();
        imgUrl = in.readString();

        bitrate = in.readInt();
        duration = in.readInt();
        isFavorite = in.readInt() == 1;
        isNotInDevice = in.readInt() == 1;
        isPublicSong = in.readInt() == 1;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeString(fileName);
        out.writeString(fileType);
        out.writeString(fileUrl);
        out.writeString(fileMd5);
        out.writeLong(fileSize);
        out.writeLong(dateAdded);

        out.writeString(title);
        out.writeString(artist);
        out.writeString(album);
        out.writeString(year);
        out.writeString(genre);
        out.writeString(lyrics);
        out.writeString(imgUrl);

        out.writeInt(bitrate);
        out.writeInt(duration);
        out.writeInt(isFavorite ? 1 : 0);
        out.writeInt(isNotInDevice ? 1 : 0);
        out.writeInt(isPublicSong ? 1 : 0);
    }
}
